package Watcher.youtube;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.google.gdata.client.youtube.YouTubeQuery;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.VideoFeed;
import com.google.gdata.util.ServiceException;

public class YoutubeQuerys {
	public static void main(String[] args) {
		String developerId = "AI39si4qD2O0x-iIu2L-TrzELTuAOpl8grPSfQfrFCU-cp8-fpXkUF2cotY4XRNggZUG_pK11wMKV5wZ50rIZgV4jHTnIA64ZQ";
		YouTubeService service = new YouTubeService("", developerId);

		URL query;
		VideoFeed videoFeed = null;
		try {
			// query = new
			// URL("http://gdata.youtube.com/feeds/api/videos/vbXR38GZ1QE");
			query = new URL(
					"http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed");
			videoFeed = service.getFeed(query, VideoFeed.class);
			printVideoFeed(videoFeed, true);

			YouTubeQuery tubeQuery = new YouTubeQuery(new URL(
					"http://gdata.youtube.com/feeds/api/videos"));
			tubeQuery.setFullTextQuery("justin bieber");
			tubeQuery.setSafeSearch(YouTubeQuery.SafeSearch.NONE);
			VideoFeed videoFeed2 = service.query(tubeQuery, VideoFeed.class);
			printVideoFeed(videoFeed2, true);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		VideoEntry videoEntry = videoFeed.getEntries().get(0);
	}

	public static void printVideoFeed(VideoFeed videoFeed, boolean detailed) {
		for (VideoEntry videoEntry : videoFeed.getEntries()) {
			printVideoEntry(videoEntry, detailed);
		}
	}

	private static void printVideoEntry(VideoEntry videoEntry, boolean detailed) {
		System.out.println("Title: " + videoEntry.getTitle().getPlainText());
		videoEntry.getHtmlLink();
	}
}
